package ro.isdc.navigator.page;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.AbstractReadOnlyModel;

import ro.isdc.navigator.component.view.AbstractPageNavigatorView;
import ro.isdc.navigator.factory.SearchContext;
import ro.isdc.navigator.professor.domain.Professor;

/**
 * Index page.
 * @author Andrei Chirila / ISDC! Romania
 * @version $Revision: 1.0 $
 * @date Jul 17, 2009
 */
public class Index extends BasePage {

  /**
   * Page size
   */
	private static final int PAGE_SIZE = 2;

	/**
	 * The paging navigator component
	 */
	private PagingNavigator pagingNavigator;

	/**
	 * The pageable view
	 */
	private AbstractPageNavigatorView<Professor> pagedDataView;

	/**
	 * The paging container.
	 */
	private WebMarkupContainer pagingContainer;
	
	/**
	 * The header of the paged view.
	 */
	private WebMarkupContainer pagedViewHeader;

	/**
	 * Constructor of page
	 */
	public Index() {
		setOutputMarkupId(true);
		addComponents();
	}

	/**
	 * Added components.
	 */
	private void addComponents() {
		add(getPagingContainer());
	}

	/**
	 * @return the paging container
	 */
	private WebMarkupContainer getPagingContainer() {
		if (pagingContainer == null) {
			pagingContainer = new WebMarkupContainer("pagingContainer");
			pagingContainer.setOutputMarkupId(true);
			pagingContainer.add(getPagedViewHeader());
			pagingContainer.add(getPagedDataView());
			pagingContainer.add(getPagingNavigator());
		}

		return pagingContainer;
	}
	
	/**
	 * @return the paged view header
	 */
	private WebMarkupContainer getPagedViewHeader() {
	  if (pagedViewHeader == null) {
	    pagedViewHeader = new WebMarkupContainer("pagedViewHeader");
	    pagedViewHeader.add(new Label("firstNameLabel", "First Name"));
	    pagedViewHeader.add(new Label("lastNameLabel", "Last Name"));
	    pagedViewHeader.add(new Label("sexLabel", "Sex"));
	    pagedViewHeader.add(new Label("dateOfBirthLabel", "Date Of Birth"));
	    pagedViewHeader.add(new Label("SSNLabel", "SSN"));
	    pagedViewHeader.setOutputMarkupId(true);
	  }
	  
	  return pagedViewHeader;
	}

	/**
	 * @return the pageable view
	 */
	@SuppressWarnings("serial")
	private AbstractPageNavigatorView<Professor> getPagedDataView() {
		if (pagedDataView == null) {
			SearchContext searchContext = new SearchContext();
			searchContext.setPageSize(PAGE_SIZE);
			pagedDataView = new AbstractPageNavigatorView<Professor>("pagedView",
					getBeanProviderFacade().getProfessorPagesNavigatorFactory()
							.createPagesNavigator(searchContext)) {
        @Override
				protected void populateItem(final Item<Professor> item) {
				  Professor professor = item.getModelObject();
				  item.add(new Label("firstName", professor.getFirstName()));
				  item.add(new Label("lastName", professor.getLastName()));
				  item.add(new Label("sex", professor.getSexType().toString()));
				  item.add(new Label("dateOfBirth", professor.getDateOfBirth().toString()));
				  item.add(new Label("SSN", professor.getSSN()));
				  item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel<String>()
          {
              public String getObject()
              {
                  return (item.getIndex() % 2 == 1) ? "even" : "odd";
              }
          }));
				}
			};
			pagedDataView.setOutputMarkupId(true);
		}
		return pagedDataView;
	}

	/**
	 * @return the paging container
	 */
	private PagingNavigator getPagingNavigator() {
		if (pagingNavigator == null) {
			pagingNavigator = new AjaxPagingNavigator("pagingNavigator", getPagedDataView());
		}

		return pagingNavigator;
	}
}
